<?php 
/*
Plugin Name: Hatforrent Plugin
Plugin URI: http://www.hatforrent.com
Description: Run services to get content from hatforrent
Author: HoanBN
Version: 1.0
Author URI: http://www.hatforrent.com
*/  
session_start();
global $hfr_db_version;
$plugin_version = '1.0';
$hfr_db_version = '0.1.0';

define("HFR_SERVER", "http://beta.hatforrent.com");
define("HFR_PATH", plugin_dir_url( __FILE__ ));

/******************************
* includes
******************************/

include('includes/functions.php');

if(isset($_GET['act']) && $_GET['act'] == 'submit_data') {
	if(isset($_GET['holder']) && $_GET['holder'] == 'hfr_sources')
		include('includes/admin/source_submit.php');
} 

include ('includes/admin/source_ajax.php');
include('includes/admin-page.php');

register_activation_hook(__FILE__, 'hfr_active');

function hfr_active($networkwide) {
	//include ('includes/install/install.sql.php');
	global $wpdb;
	global $hfr_db_version;
	update_option("hfr_db_version", $hfr_db_version);
	
	if (function_exists('is_multisite') && is_multisite()) {
        // check if it is a network activation - if so, run the activation function for each blog id
        if ($networkwide) {
            $old_blog = $wpdb->blogid;
            // Get all blog ids
            $blogids = $wpdb->get_col("SELECT blog_id FROM $wpdb->blogs");
            foreach ($blogids as $blog_id) {
                switch_to_blog($blog_id);
                hfr_create_metadata_table();
            }
            switch_to_blog($old_blog);
            return;
        }  
    }
    hfr_create_metadata_table();
}

function hfr_deactive($networkwide) {
	global $wpdb;
	delete_option("hfr_db_version");
	
	if (function_exists('is_multisite') && is_multisite()) {
        // check if it is a network activation - if so, run the activation function for each blog id
        if ($networkwide) {
            $old_blog = $wpdb->blogid;
            // Get all blog ids
            $blogids = $wpdb->get_col("SELECT blog_id FROM $wpdb->blogs");
            foreach ($blogids as $blog_id) {
                switch_to_blog($blog_id);
                hfr_remove_metadata_table();
            }
            switch_to_blog($old_blog);
            return;
        }  
    }
    
    hfr_remove_metadata_table();
}

register_deactivation_hook(__FILE__, 'hfr_deactive');

function hfr_create_metadata_table() {
	require_once(ABSPATH . '/wp-admin/includes/upgrade.php');
	global $wpdb;
	$sql = 
	"
	CREATE TABLE IF NOT EXISTS `".$wpdb->prefix."hfr_url_craw` (
	  `id` varchar(255) NOT NULL,
	  `sitemap` varchar(255) NOT NULL,
	  `userBlog` varchar(255) NOT NULL,
	  `blog` varchar(255) NOT NULL,
	  `reglink` varchar(255) NOT NULL,
	  `type` varchar(255) NOT NULL,
	  `domain` varchar(255) NOT NULL,
	  `tag` varchar(255) NOT NULL,
	  `linkSelecterJs` text NOT NULL,
	  `activeLinkJs` varchar(255) NOT NULL default 'no',
	  `linkSelecter` varchar(255) NOT NULL,
	  `titleSelecter` varchar(255) NOT NULL,
	  `contentSelecter` varchar(255) NOT NULL,
	  `removeSelecter` varchar(255) NOT NULL,
	  `customSelecter` text NOT NULL,
	  `category` varchar(255) NOT NULL,
	  `typelink` varchar(255) NOT NULL,
	  `source` varchar(255) NOT NULL,
	  `sourcetype` varchar(255) NOT NULL,
	  `source_image` varchar(255) NOT NULL,
	  `remove_tag_a` varchar(255) NOT NULL,
	  `activeContentJs` varchar(255) NOT NULL default 'no',
	  `md5` varchar(255) NOT NULL default 'no',
	  `timeCraw` int(11) NOT NULL default '72',
	  `error` int(11) NOT NULL default '0',
	  `typeError` varchar(255) NOT NULL default 'No Error',
	  `timeError` float NOT NULL,
	  `time` float NOT NULL,
	  `active` int(11) NOT NULL default '1',
	  `field` varchar(255) NOT NULL,
	  `order` int(11) NOT NULL default '0',
	  `share` varchar(255) NOT NULL default 'yes',
	  `craw` varchar(255) NOT NULL default 'yes',
	  `getAll` varchar(255) NOT NULL default 'no',
	  `arrLink` varchar(255) NOT NULL,
	  UNIQUE KEY `id` (`id`)
	) ENGINE=MyISAM DEFAULT CHARSET=utf8;
	";
	dbDelta($sql);
	
	
	$sql = 
	"
	CREATE TABLE IF NOT EXISTS `".$wpdb->prefix."hfr_payment` (
	  `id` varchar(32) NOT NULL,
	  `customer` varchar(32) NOT NULL,
	  `price` int(11) NOT NULL,
	  `content` text NOT NULL,
	  `created` datetime NOT NULL,
	  `start` datetime NOT NULL,
	  `end` datetime NOT NULL,
	  PRIMARY KEY  (`id`)
	) ENGINE=MyISAM DEFAULT CHARSET=utf8;
	";
	dbDelta($sql);
}

function hfr_remove_metadata_table() {
	require_once(ABSPATH . '/wp-admin/includes/upgrade.php');
	global $wpdb;
	$sql = "DROP TABLE IF EXISTS `".$wpdb->prefix."hfr_url_craw`";
	$wpdb->query($sql);
	
	$sql = "DROP TABLE IF EXISTS `".$wpdb->prefix."hfr_payment`";
	$wpdb->query($sql);
}


add_action( 'wpmu_new_blog', 'hfr_new_blog', 10, 6);        
 
function hfr_new_blog($blog_id, $user_id, $domain, $path, $site_id, $meta ) {
    global $wpdb;
 
    if (is_plugin_active_for_network('hatforrent/hatforrent.php')) {
        $old_blog = $wpdb->blogid;
        switch_to_blog($blog_id);
        hfr_create_metadata_table();
        switch_to_blog($old_blog);
    }
}

add_action( 'delete_blog', 'hfr_delete_blog', 10, 2);

function hfr_delete_blog($blog_id) {
	global $wpdb;
 
    if (is_plugin_active_for_network('hatforrent/hatforrent.php')) {
        $old_blog = $wpdb->blogid;
        switch_to_blog($blog_id);
        hfr_remove_metadata_table();
        switch_to_blog($old_blog);
    }
}